package com.tour.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Poor;
import com.tour.mapper.PoorMapper;
import com.tour.service.PoorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-01-20
 */
@Service
public class PoorServiceImpl extends ServiceImpl<PoorMapper, Poor> implements PoorService {

    @Autowired
    PoorMapper poorMapper;

    public ResponseWrapper addPoor(Poor poor){
        if (poor == null){
            return ResponseWrapper.markError();
        }else {
            int result = poorMapper.insert(poor);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper delPoor(Integer poorId){
        if (poorId == null){
            return ResponseWrapper.markError();
        }else {
            int result = poorMapper.deleteById(poorId);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper updatePoor(Poor poor){
        if (poor == null){
            return ResponseWrapper.markError();
        }else {
            int result = poorMapper.updateById(poor);
            if (result == 1){
                return ResponseWrapper.markSuccess(null);
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
    public ResponseWrapper findAllPoor(String poorContent,Integer pageSize,Integer pageNum){
        QueryWrapper<Poor> poorQueryWrapper = new QueryWrapper<>();
        poorQueryWrapper.like("poor_content",poorContent).or().like("poor_name",poorContent).orderByDesc("poor_grade").orderByDesc("poor_id");
        Page<Poor> page = new Page<>(pageNum,pageSize);
        IPage<Poor> poorIPage = poorMapper.selectPage(page,poorQueryWrapper);
        List list = poorIPage.getRecords();
        ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
        wrapper.getData().put("data",list);
        wrapper.getData().put("total",poorIPage.getTotal());
        wrapper.getData().put("pageSize",poorIPage.getSize());
        wrapper.getData().put("pageNum",poorIPage.getCurrent());
        return wrapper;
    }

    public ResponseWrapper findPoor(Integer poorId,Integer pageSize,Integer pageNum){
        Poor poor = poorMapper.selectById(poorId);
        if (poor == null){
            return ResponseWrapper.markCustom(false,500,"没有该用户",null);
        }else {
            Page<Map> page = new Page<>(pageNum,pageSize);
            IPage<Map> poorIPage = poorMapper.findPoor(page,poorId);
            List<Map> list = poorIPage.getRecords();
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(poor);
            wrapper.getData().put("data",poor);
            wrapper.getData().put("targets",list);
            wrapper.getData().put("total",poorIPage.getTotal());
            wrapper.getData().put("pageSize",poorIPage.getSize());
            wrapper.getData().put("pageNum",poorIPage.getCurrent());
            return wrapper;
        }
    }


}
